{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.3"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 逻辑运算符\n",
    "\n",
    "逻辑运算符主要用来做一些逻辑判断"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## `not` 逻辑非\n",
    "\n",
    "- 一元运算符\n",
    "- 对符号右侧的值进行非运算\n",
    "- 对于布尔值，非运算会对其进行取反操作，`True` -> `False`、`False` -> `True`\n",
    "- 对于非布尔值，非运算会先将其转换为布尔值，然后再取反"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True\n",
    "b = 1\n",
    "c = 0\n",
    "d = None\n",
    "a = not a\n",
    "b = not b\n",
    "c = not c\n",
    "d = not d\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')"
   ]
  },
  {
   "source": [
    "## `and` 逻辑与\n",
    "\n",
    "- 二元运算符\n",
    "- 对符号两侧的值进行与运算\n",
    "- 只有在符号两侧的值都为 `True`，才会返回 `True`，只要有一个 `False` 就返回 `False`\n",
    "- 与运算找 `False`\n",
    "- Python 中的与运算是短路与，如果第一个值为 `False`，则不看第二个值"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True and True\n",
    "b = True and False\n",
    "c = False and True\n",
    "d = False and False\n",
    "e = 1 and True\n",
    "f = True and print('Hello')\n",
    "g = False and print('World')\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')\n",
    "print(f'e = {e}')"
   ]
  },
  {
   "source": [
    "## `or` 逻辑或\n",
    "\n",
    "- 二元运算符\n",
    "- 对符号两侧的值进行与运算\n",
    "- 或运算两个值中只要有一个 `True`，就会返回 `True`\n",
    "- 或运算找 `True`\n",
    "- Python 中的或运算是短路或，如果第一个值为 `False`，则不看第二个值"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True or True\n",
    "b = True or False\n",
    "c = False or True\n",
    "d = False or False\n",
    "e = True or 1\n",
    "f = True or print('Hello')\n",
    "g = False or print('World')\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')\n",
    "print(f'e = {e}')"
   ]
  },
  {
   "source": [
    "## 非布尔值的与或运算\n",
    "\n",
    "做非布尔值的与或运算时，Python 会将其当作布尔值运算，最终会返回原值"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1 and 2\n",
    "b = 2 and 1\n",
    "c = 0 and 1\n",
    "d = None and 1\n",
    "e = '' and 1\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "print(f'c = {c}, type = {type(c)}')\n",
    "print(f'd = {d}, type = {type(d)}')\n",
    "print(f'e = {e}, type = {type(e)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1 or 2\n",
    "b = 2 or 1\n",
    "c = 0 or 1\n",
    "d = None or '1'\n",
    "e = '' or 1\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "print(f'c = {c}, type = {type(c)}')\n",
    "print(f'd = {d}, type = {type(d)}')\n",
    "print(f'e = {e}, type = {type(e)}')"
   ]
  }
 ]
}